/*
 * AdminViewImpl.java
 */
package ATBMS.view.user;

import ATBMS.modles.Flight;
import ATBMS.control.MainApp;
import ATBMS.control.UserControl;
import ATBMS.modles.Ticket;
import ATBMS.modles.User;
import ATBMS.view.useredit.EditUserViewImpl;
import ATBMS.view.useredit.UserEditViewInterface;
import java.text.SimpleDateFormat;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * The application's main frame.
 */
public class UserViewImpl extends FrameView
        implements UserViewInterface {

    private int tableMode;
    private DefaultTableModel flightModle;
    private DefaultTableModel ticketModle;
    private UserControl uc;


    public boolean confirm(String s) {
        return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, s, "Please Confirm", JOptionPane.YES_NO_OPTION);
    }

    public String inputDialog(String s) {
        return JOptionPane.showInputDialog(s);
    }

    public int getTableMode() {
        return tableMode;
    }

    public void setTableMode(int mode) {
        tableMode = mode;
        switch (tableMode) {
            case USERVIEWTABLEMODE_FLIGHTS:
                mainTable.setModel(flightModle);
                break;
            case USERVIEWTABLEMODE_TICKET:
                mainTable.setModel(ticketModle);
                break;
            default:
                throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    public String getSelectedFlightCode() {
        int sel = mainTable.getSelectedRow();
        if (sel == -1) {
            return "";
        }
        if (tableMode == USERVIEWTABLEMODE_FLIGHTS) {
            return (String) flightModle.getValueAt(sel, 1);
        } else if (tableMode == USERVIEWTABLEMODE_TICKET) {
            return (String) ticketModle.getValueAt(sel, 0);
        }
        return "";
    }

    public void alert(String s) {
        JOptionPane.showMessageDialog(null, s);
    }

    public void hide() {
        MainApp.getApplication().hide(this);
    }

    public void show() {
        MainApp.getApplication().show(this);
    }

    public void listTickets(List<Ticket> tickets) {
        ticketModle.setRowCount(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Ticket t : tickets) {
            ticketModle.addRow(new Object[]{
                        t.getFcode(),
                        dateFormat.format(t.getBooktime()),
                        t.getUtel()
                    });
        }
    }

    public void listFlight(List<Flight> flights) {
        //Clear data
        flightModle.setRowCount(0);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //Add new data to usersModel
        for (Flight f : flights) {
            String gooffString = dateFormat.format(f.getFgooff());
            String arrivalString = dateFormat.format(f.getFarrival());
            flightModle.addRow(new Object[]{
                        f.getFdest(),
                        f.getFcode(),
                        f.getFairline(),
                        gooffString,
                        arrivalString,
                        f.getFbusload(),
                        f.getFequip(),
                        f.getFprice()
                    });
        }
    }

    public UserViewImpl(SingleFrameApplication app, UserControl uc) {
        super(app);
        this.uc = uc;
        flightModle = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        ticketModle = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        flightModle.addColumn("Destnation");
        flightModle.addColumn("Flight");
        flightModle.addColumn("Airline");
        flightModle.addColumn("Go-Off");
        flightModle.addColumn("Arrival");
        flightModle.addColumn("Busload");        
        flightModle.addColumn("Equip.");
        flightModle.addColumn("Price");

        ticketModle.addColumn("Flight");
        ticketModle.addColumn("BookTime");
        ticketModle.addColumn("Telephone");

        initComponents();
        getFrame().addWindowListener(new java.awt.event.WindowAdapter() {

            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                MainApp.getApplication().exit();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainTable = new javax.swing.JTable();
        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        newUserMenuItem = new javax.swing.JMenuItem();
        editFlightMenuItem = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mainTable.setModel(ticketModle);
        mainTable.setName("mainTable"); // NOI18N
        mainTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        mainTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(mainTable);
        mainTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ATBMS.control.MainApp.class).getContext().getResourceMap(UserViewImpl.class);
        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem4.setAction(uc.getAccountMenuAction());
        jMenuItem4.setText(resourceMap.getString("jMenuItem4.text")); // NOI18N
        jMenuItem4.setName("jMenuItem4"); // NOI18N
        jMenu1.add(jMenuItem4);

        jMenuItem3.setAction(uc.getAccountMenuAction());
        jMenuItem3.setText(resourceMap.getString("jMenuItem3.text")); // NOI18N
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenu1.add(jMenuItem3);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ATBMS.control.MainApp.class).getContext().getActionMap(UserViewImpl.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        jMenu1.add(exitMenuItem);

        menuBar.add(jMenu1);

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        newUserMenuItem.setAction(uc.getTicktetsMenuAction());
        newUserMenuItem.setText(resourceMap.getString("newUserMenuItem.text")); // NOI18N
        newUserMenuItem.setName("newUserMenuItem"); // NOI18N
        fileMenu.add(newUserMenuItem);

        editFlightMenuItem.setAction(uc.getTicktetsMenuAction());
        editFlightMenuItem.setText(resourceMap.getString("editFlightMenuItem.text")); // NOI18N
        editFlightMenuItem.setName("editFlightMenuItem"); // NOI18N
        fileMenu.add(editFlightMenuItem);

        jMenuItem1.setAction(uc.getTicktetsMenuAction());
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        jMenuItem2.setAction(uc.getTicktetsMenuAction());
        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        fileMenu.add(jMenuItem2);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("about")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem editFlightMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTable mainTable;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newUserMenuItem;
    // End of variables declaration//GEN-END:variables
}
